<?php

namespace App\Admin\Actions\ShopProduct;

use App\Library\Logic\OperateLogLogic;
use App\Library\Logic\ShopCategoryLogic;
use App\Library\Models\AdminShopProductModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;

class ShopProductCateAction extends RowAction
{
    public $name = '分类';

    /**
     * handle
     * @param AdminShopProductModel $model
     * @param Request $request
     * @return Response
     */
    public function handle(AdminShopProductModel $model, Request $request): Response
    {
        $model->product_category_id_1 = $request->product_category_one;
        $model->product_category_id_2 = $request->product_category_two;
        $model->product_category_id_3 = $request->product_category_three;
        $model->save();
        OperateLogLogic::add(Admin::user()->user_id, 'admin.product.cateAction');
        return $this->response()->success('操作成功')->refresh();
    }

    /**
     * handle
     * @param AdminShopProductModel $model
     * @return void
     */
    public function form(AdminShopProductModel $model): void
    {
        $this->select('product_category_one', '一级分类')->options(__('base.cate'))->required()
            ->default($model->product_category_id_1);
        $this->select('product_category_two', '二级分类')->default($model->product_category_id_2)
            ->options(ShopCategoryLogic::getByParentId($model->product_category_id_1))->required();
        $this->select('product_category_three', '三级分类')->default($model->product_category_id_3)->required()
            ->options(ShopCategoryLogic::getByParentId($model->product_category_id_2));
    }
}
